local extension = Package:new("huixiangyiwei")
extension.extensionName = "huixiang"

Fk:loadTranslationTable{
    ["huixiangyiwei"] = "忆·魏",
    ["hx_yi"] = "忆",
}

Fk:appendKingdomMap("god", {"hx_yi"})

local simayi = General:new(extension, "yi__simayi","hx_yi",3,4)
Fk:loadTranslationTable{
    ["yi__simayi"] = "忆司马懿",
    ["#yi__simayi"] = "顾前视忆",
    ["designer:yi__simayi"] = "庆思书",
    ["yi__langyi"] = "狼意",
    [":yi__langyi"] = "你使用牌指定目标/成为其他角色使用牌的目标时，若你未记录此牌牌名，则你可以永久记录之；反之，你可以进行一次判定。",
    ["yi__yiji"] = "忆己",
    [":yi__yiji"] = "一张判定牌即将生效时/你受到1点伤害后，你可以执行一项：1.摸两张牌；2.获得一名其他角色区域内一张牌并获得<font color='purple'>【鬼才】</font>直到本轮结束；3.移去一个已记录的牌名然后回复一点体力。",
    ["yi__guiyi"] = "归忆",
    ["@yi__guiyi"] = "归忆",
    [":yi__guiyi"] = "限定技，每个回合结束时，若【狼意】记录的牌名数不小于你体力上限的两倍，你可以记录所有牌名然后执行一个额外回合，若如此做，本轮结束后你失去【狼意】。",
    ["$yi__langyi1"] = "我掌天命，命数可改！",
    ["$yi__langyi2"] = "天命，尽在我手中！",
    ["$yi__langyi3"] = "哼，作法自毙。",
    ["$yi__langyi4"] = "哼，尽在我的掌握。",
    ["$yi__yiji1"] = "哼，我怎么会空手而归？",
    ["$yi__yiji2"] = "动我分毫，必有所失。",
    ["$yi__guiyi1"] = "别挣扎了，一切都是徒劳的。",
    ["$yi__guiyi2"] = "这，就是你违抗不了的天命！",
    ["~yi__simayi"] = "天煞孤星，这都是命中注定的吗",
}

local yi__langyi = fk.CreateTriggerSkill {
  name = "yi__langyi",
  events = {fk.CardUsing, fk.TargetSpecifying,fk.TargetConfirming},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self.name)) then return false end
    if not data.card then return false end
    local recorded = type(player:getMark("@langyi")) == "table" and 
                player:getMark("@langyi") or {}
    local cardName = data.card.trueName
    local condition = not table.contains(recorded, cardName)
    if event == fk.TargetSpecifying then
      return data.firstTarget 
    elseif event == fk.TargetConfirming then
      return data.from ~= player.id 
    end
    return false
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cardName = data.card.trueName
    local recorded = player:getMark("@$yi__langyi")
    recorded = type(recorded) == "table" and recorded or {}  
    if not table.contains(recorded, cardName) then
      if room:askForSkillInvoke(player, self.name, data, "#yi__langyi-record:::"..cardName) then
        table.insert(recorded, cardName)
        room:setPlayerMark(player, "@$yi__langyi", recorded)
        return true
      end
    else
      if room:askForSkillInvoke(player, self.name, data, "#yi__langyi-judge:::"..cardName) then
        local judge = {
          who = player,
          reason = self.name,
          pattern = ".|.|.",
        }
        room:judge(judge)
        return true
      end
    end
    return false
  end,

}

Fk:loadTranslationTable{
  ["yi__langyi"] = "狼意",
  ["#yi__langyi-record"] = "是否记录【%arg】的牌名？",
  ["#yi__langyi-judge"] = "已记录【%arg】，是否进行判定？",
  ["@$yi__langyi"] = "已记录的牌名",
}

simayi:addSkill(yi__langyi)

local yi__yiji = fk.CreateTriggerSkill{
  name = "yi__yiji",
  events = {fk.AskForRetrial, fk.Damaged},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.AskForRetrial then
      return true 
    elseif event == fk.Damaged then
      return target == player
    end
    return false
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {"yi__yiji1","yi__yiji2"}
    local marks = player:getMark("@$yi__langyi")
    if type(marks) == "table" and #marks > 0 then
      table.insert(choices, "yi__yiji3")
    end
    local choice = player.room:askForChoice(player, choices, self.name, "#yi__yiji-choose")
    if choice then
      self.cost_data = choice
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == "yi__yiji1" then
      player:drawCards(2, self.name)
    elseif self.cost_data == "yi__yiji2" then
      if player:hasSkill(self) then
        local room = player.room
        local to = room:askForChoosePlayers(player, table.map(
        table.filter(room:getOtherPlayers(player),
          function(p) return #p:getCardIds { Player.Hand, Player.Equip, Player.Judge } > 0 end), Util.IdMapper), 1, 1,
      "忆己：你可以获得一名其他角色区域内的一张牌", self.name)
        if #to > 0 then
          self.cost_data = to[1]
          local to = room:getPlayerById(self.cost_data)
          local cid = room:askForCardChosen(player, to, "hej", self.name)
          if cid then
            room:moveCardTo(cid, Player.Hand, player, fk.ReasonPrey, self.name, nil, false, player.id)
          end
        end
        if not player.dead and not player:hasSkill("yisimayi__guicai", true) then
          room:handleAddLoseSkills(player, "yisimayi__guicai", nil, true, false)
          room.logic:getCurrentEvent():findParent(GameEvent.Round):addCleaner(function()
            room:handleAddLoseSkills(player, "-yisimayi__guicai", nil, true, false)
          end)
        end
      end
    elseif self.cost_data == "yi__yiji3" then
      local marks = player:getMark("@$yi__langyi")
      if type(marks) == "table" and #marks > 0 then
        local toRemove = room:askForChoice(player, marks, self.name, "#yi__yiji-remove")
        table.removeOne(marks, toRemove)
        room:setPlayerMark(player, "@$yi__langyi", #marks > 0 and marks or 0)
        room:recover({
          who = player,
          num = 1,
          skillName = self.name
        })
      end
    end
  end
}

simayi:addSkill(yi__yiji)

Fk:loadTranslationTable{
  ["#yi__yiji-choose"] = "忆己：请选择一项执行",
  ["yi__yiji1"] = "摸两张牌",
  ["yi__yiji2"] = "获得一名其他角色区域内一张牌并获得【鬼才】直到本轮结束",
  ["yi__yiji3"] = "移去一个已记录的牌名然后回复一点体力",
}

local yi__guiyi = fk.CreateTriggerSkill{
  name = "yi__guiyi",
  events = {fk.TurnEnd},
  frequency = Skill.Limited,
  limit_mark = "@yi__guiyi",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(self.name, Player.HistoryGame) > 0 then
      return false
    end
    local marks = player:getMark("@$yi__langyi")
    marks = type(marks) == "table" and marks or {}
    return player:hasSkill(self) and #marks >= player.maxHp * 2
end,

  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local allCardNames = {}
    local nameSet = {}
    for _, id in ipairs(room.draw_pile) do
      local card = Fk:getCardById(id)
      if not nameSet[card.name] then
        table.insert(allCardNames, card.name)
        nameSet[card.name] = true
      end
    end
    for _, id in ipairs(room.discard_pile) do
      local card = Fk:getCardById(id)
      if not nameSet[card.name] then
        table.insert(allCardNames, card.name)
        nameSet[card.name] = true
      end
    end
    room:setPlayerMark(player, "@$yi__langyi", allCardNames)
    player:gainAnExtraTurn()
    if player:hasSkill("yi__langyi", true) then
      room:handleAddLoseSkills(player, "yi__langyi", nil, true, false)
      room.logic:getCurrentEvent():findParent(GameEvent.Round):addCleaner(function()
        room:handleAddLoseSkills(player, "-yi__langyi", nil, true, false) 
      end)
    end
  end
}




local yisimayi__guicai = fk.CreateTriggerSkill{
  name = "yisimayi__guicai",
  anim_type = "control",
  events = {fk.AskForRetrial},
  can_trigger = function(self, event, target, player, data)
      return player:hasSkill(self.name) and not player.dead
  end,
  on_cost = function(self, event, target, player, data)
      local room = player.room
      return player.room:askForSkillInvoke(player, self.name, data,"<font color='purple'>鬼才</font>：是否选择一个花色，用牌堆中的随机一张该花色的牌代替判定牌（然后你可以从牌堆中获得一张牌名与你已记录的牌名相同的牌并弃置两张牌）？")
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local suits = {"spade", "heart", "club", "diamond"}
      local marks = player:getMark("@$yi__langyi")
      local suit = room:askForChoice(player, suits, self.name, "#yisimayi__guicai-suit")
      local card = room:askForChoice(player, marks, self.name, "#yisimayi__guicai-card")
      local cardFound = nil
      local card2Found = nil
      for _, id in ipairs(room.draw_pile) do
          local card11 = Fk:getCardById(id)
          if card11:getSuitString() == suit and not cardFound then
              cardFound = id
          end
          if card11.trueName == card and not card2Found then
              card2Found = id
          end
      end
      if cardFound then
          local thecard = Fk:getCardById(cardFound)
          if thecard then
              self.cost_data = thecard
              player.room:retrial(self.cost_data, player, data, self.name)
              room:sendLog{
                  type = "#yisimayi__guicai_change1",
                  from = player.id,
                  arg = cardFound,
              }
          end
      end
      if card2Found then
        room:obtainCard(player, card2Found, false, fk.ReasonPrey)
        room:sendLog{
            type = "#yisimayi__guicai_change2",
            from = player.id,
        }
        room:askForDiscard(player, 2, 2, true, self.name, false)
      end
  end
}

Fk:loadTranslationTable{
  ["#yisimayi__guicai_change1"] = "%from用牌堆中的一张其选择的花色的牌代替了判定牌。",
  ["#yisimayi__guicai_change2"] = "%from从牌堆中获得了一张其选择的牌名的牌。",
}

Fk:loadTranslationTable{
  ["yisimayi__guicai"] = "<font color='purple'>鬼才</font>",
  [":yisimayi__guicai"] = "当一名角色的判定牌生效前，你可以用牌堆中的随机一张你选择的花色的牌代替之，若如此做，你可以从牌堆中获得一张牌名与你已记录的牌名相同的牌并弃置两张牌。",
  ["#yisimayi__guicai-suit"] = "请选择花色",
  ["#yisimayi__guicai-card"] = "请选择牌名",
  ["$yisimayi__guicai"] = "逆天改运，我即天命！",
}



simayi:addSkill(yi__guiyi)
simayi:addRelatedSkill(yisimayi__guicai)



return extension
